﻿using System;
using System.Web;
using System.Web.Services;
using Dost.Khcn.Biz;
using Dost.Khcn.Data.EntityModel;
using Dost.Khcn.Web.Common;
using Pechkin;
using Pechkin.Synchronized;

namespace Dost.Khcn.Web.Admin
{
    public partial class ChuyenGiaDetails : System.Web.UI.Page
    {
        #region Properties

        private static ChuyenGiaBiz _chuyenGiaBiz;
        private static ChuyenGiaBiz ChuyenGiaManager
        {
            get
            {
                return (_chuyenGiaBiz ?? (_chuyenGiaBiz = new ChuyenGiaBiz()));
            }
        }

        private static QuaTrinhCongTacBiz _quaTrinhCongTacBiz;
        private static QuaTrinhCongTacBiz QuaTrinhCongTacManager
        {
            get
            {
                return (_quaTrinhCongTacBiz ?? (_quaTrinhCongTacBiz = new QuaTrinhCongTacBiz()));
            }
        }

        private static CongTrinhNghienCuuBiz _qaCongTrinhNghienCuuBiz;
        private static CongTrinhNghienCuuBiz CongTrinhNghienCuuManager
        {
            get
            {
                return (_qaCongTrinhNghienCuuBiz ?? (_qaCongTrinhNghienCuuBiz = new CongTrinhNghienCuuBiz()));
            }
        }

        private static DeTaiCapNhaNuocBiz _qaDeTaiCapNhaNuocBiz;
        private static DeTaiCapNhaNuocBiz DeTaiCapNhaNuocManager
        {
            get
            {
                return (_qaDeTaiCapNhaNuocBiz ?? (_qaDeTaiCapNhaNuocBiz = new DeTaiCapNhaNuocBiz()));
            }
        }

        private static DeTaiCapTinhBiz _qaDeTaiCapTinhBiz;
        private static DeTaiCapTinhBiz DeTaiCapTinhManager
        {
            get
            {
                return (_qaDeTaiCapTinhBiz ?? (_qaDeTaiCapTinhBiz = new DeTaiCapTinhBiz()));
            }
        }

        private static GiaiThuongKhcnBiz _qaGiaiThuongKhcnBiz;
        private static GiaiThuongKhcnBiz GiaiThuongKhcnManager
        {
            get
            {
                return (_qaGiaiThuongKhcnBiz ?? (_qaGiaiThuongKhcnBiz = new GiaiThuongKhcnBiz()));
            }
        }

        private static ChuyenGiaFileBiz _chuyenGiaFileBiz;
        private static ChuyenGiaFileBiz ChuyenGiaFileManager
        {
            get { return _chuyenGiaFileBiz ?? (_chuyenGiaFileBiz = new ChuyenGiaFileBiz()); }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (WebCommon.CurrrentEmployee == null)
            {
                Response.Redirect("~/NoPermission.aspx");
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            int chuyenGiaId;
            int.TryParse(Request.Params["id"], out chuyenGiaId);
            var chuyenGia = (ChuyenGiaDetailView)ChuyenGiaManager.GetInfoDetailsById(chuyenGiaId);
            if (chuyenGia == null) return;

            switch (ddlExportType.SelectedValue)
            {
                case "1": // word
                    var html = ChuyenGiaManager.GenerateHtm(chuyenGia);
                    Response.Clear();
                    Response.Buffer = true;
                    Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(chuyenGia.HoTen) + ".doc");
                    Response.ContentEncoding = System.Text.Encoding.UTF8;
                    Response.ContentType = "application/vnd.ms-word";
                    Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
                    var bytes = System.Text.Encoding.Unicode.GetBytes(html);
                    Response.BinaryWrite(bytes);
                    Response.Flush();
                    Response.End();
                    break;
                case "2": // excel
                    html = ChuyenGiaManager.GenerateHtm(chuyenGia);
                    Response.Clear();
                    Response.Buffer = true;
                    Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(chuyenGia.HoTen) + ".xls");
                    Response.ContentEncoding = System.Text.Encoding.UTF8;
                    Response.ContentType = "application/vnd.ms-excel";
                    Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
                    bytes = System.Text.Encoding.Unicode.GetBytes(html);
                    Response.BinaryWrite(bytes);
                    Response.Flush();
                    Response.End();
                    break;
                case "3": // pdf
                    html = ChuyenGiaManager.GenerateHtm(chuyenGia);

                    var oc = new ObjectConfig();
                    var pdfBuf = new SynchronizedPechkin(new GlobalConfig()).Convert(oc, html);

                    Response.ContentType = "application/pdf";
                    Response.AddHeader("Content-Disposition", "attachment;" + HttpUtility.UrlEncode(chuyenGia.HoTen) + ".pdf");
                    Response.BinaryWrite(pdfBuf);

                    break;
            }
        }

        #endregion

        #region WebMethods

        [WebMethod(EnableSession = true)]
        public static object GetChuyenGia(int id)
        {
            var chuyengia = (ChuyenGiaDetailView)ChuyenGiaManager.GetDetailsById(id);
            if (chuyengia == null) return null;
            if (!string.IsNullOrEmpty(chuyengia.ThanhTuuKhcnKhac))
            {
                chuyengia.ThanhTuuKhcnKhac = chuyengia.ThanhTuuKhcnKhac.Replace("\n", "<br />");
            }
            return chuyengia;
        }

        [WebMethod(EnableSession = true)]
        public static object QuaTrinhCongTacList(int chuyenGiaId)
        {
            var results = QuaTrinhCongTacManager.GetQuaTrinhCongTacs(chuyenGiaId);
            return new { Result = "OK", Records = results };
        }

        [WebMethod(EnableSession = true)]
        public static object CongTrinhNghienCuuList(int chuyenGiaId)
        {
            var results = CongTrinhNghienCuuManager.GetCongTrinhNghienCuus(chuyenGiaId);
            return new { Result = "OK", Records = results };
        }

        [WebMethod(EnableSession = true)]
        public static object DeTaiCapNhaNuocList(int chuyenGiaId)
        {
            var results = DeTaiCapNhaNuocManager.GetDeTaiCapNhaNuocViews(chuyenGiaId);
            return new { Result = "OK", Records = results };
        }

        [WebMethod(EnableSession = true)]
        public static object DeTaiCapTinhList(int chuyenGiaId)
        {
            var results = DeTaiCapTinhManager.GetDeTaiCapTinhViews(chuyenGiaId);
            return new { Result = "OK", Records = results };
        }

        [WebMethod(EnableSession = true)]
        public static object GiaiThuongKhcnList(int chuyenGiaId)
        {
            var results = GiaiThuongKhcnManager.GetGiaiThuongKhcns(chuyenGiaId);
            return new { Result = "OK", Records = results };
        }

        [WebMethod(EnableSession = true)]
        public static object FileList(int chuyenGiaId)
        {
            var results = ChuyenGiaFileManager.GetFiles(chuyenGiaId);
            return new { Result = "OK", Records = results };
        }

        #endregion
    }
}